//
//  InfoOfDoctorController.m
//  FastDoctor
//
//  Created by yongzhen on 16/9/7.
//  Copyright © 2016年 yongzhen. All rights reserved.
//

#import "InfoOfDoctorController.h"
#import "YZCategoryView.h"
#import "Masonry.h"
#import "CZAdditions.h"
#import "YZdetailView.h"
#import "YZAskViewController.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+Ex.h"
#import "HMDengLuViewController.h"
#define kHeaderHeight 100
@interface InfoOfDoctorController ()<UIScrollViewDelegate>
// 顶部视图 标题

@property(weak,nonatomic)UIView *headview;
@property(nonatomic,weak)UIView *detailView;
/**
 *  滚动视图
 */
@property (weak, nonatomic) UIScrollView *scrollView;
/**
 *  中间的分类视图
 */
@property (weak, nonatomic) YZCategoryView *categoryView;
// 照片字符串
@property (nonatomic,strong)NSString * imageStr;
// 名字
@property (nonatomic,strong)NSString *  name;
// 工作
@property (nonatomic,strong)NSString * job;

@property (nonatomic,assign)BOOL sele;
@end

@implementation InfoOfDoctorController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.sele = 0;
    
}
-(void)likeBtnClick:(UIButton *)btn
{
    self.sele = !self.sele;
    btn.selected = self.sele;
    NSLog(@"%zd",btn.selected);
    if (btn.selected) {
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           
            [MBProgressHUD showSuccess:@"已成功关注"];
        });
    } else {
 
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [MBProgressHUD showSuccess:@"已经取消关注"];
        });
    }

    
    
    
    
}

-(void)setupUI
{
    // 1，标题，医生详情
    self.navigationItem.title = @"医生详情";
    UIView *headView = [[UIView alloc] init];
    
    self.headview = headView;
    [self.view addSubview:headView];
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.equalTo(@60);
    }];
    
    UIButton *likeBtn = [[UIButton alloc]init];
    [likeBtn setBackgroundImage:[UIImage imageNamed:@"五角星.png"] forState: UIControlStateNormal];
    [likeBtn setBackgroundImage:[UIImage imageNamed:@"五角星黄色填充.png"] forState:UIControlStateSelected];
    self.drLikeBtn = likeBtn;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:likeBtn];
    [likeBtn sizeToFit];
        likeBtn.frame = CGRectMake(0, 0, 28, 27);
    
    
    [_drLikeBtn addTarget:self action:@selector(likeBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    
    
    // 2,医生具体详情
    YZdetailView *detailView = [[YZdetailView alloc] init];
    detailView.nameLabel.text = self.name;
    detailView.jobLabel.text = self.job;
    detailView.iconNameString = self.imageStr;
    
    [detailView DetailViewWithString:self.imageStr nameString:self.name jobString:self.job];
//    [detailView DetailViewWithString:self.imageStr ];
    
    [self.view addSubview:detailView];
    [detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headView.mas_bottom).offset(8);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@120);
    }];
    
    
    // 3.中间的分类视图
    YZCategoryView *categoryV = [[YZCategoryView alloc] init];
    categoryV.backgroundColor = [UIColor lightGrayColor];
    // 2.2 监听事件 值改变事件
    [categoryV addTarget:self action:@selector(categoryViewValuechanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:categoryV];
    
    [categoryV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(detailView.mas_bottom);
        make.left.right.equalTo(detailView);
        make.height.mas_equalTo(45);
    }];
    
    
    // 3.底部的滚动视图 UIScrollView!
    UIScrollView *scrollV = [self setupContentView];
    
    [self.view addSubview:scrollV];
    
    [scrollV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(categoryV.mas_bottom);
        make.left.right.equalTo(categoryV);
        make.bottom.equalTo(self.view).offset(30);
        
    }];
   // UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
 //   [self.view addGestureRecognizer:pan];
    
    
    
    UIButton *askBtn = [[UIButton alloc] init];
    
    [askBtn setTitle:@"咨询医师" forState:UIControlStateNormal];
    
    [askBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    askBtn.backgroundColor = [UIColor whiteColor];
    askBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    [askBtn sizeToFit];
    [self.view addSubview:askBtn];
    [askBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-10);
    }];
#pragma mark - 下面是弹出聊天的
//    [askBtn addTarget:self action:@selector(askDoctor:) forControlEvents:UIControlEventTouchUpInside];
    
#pragma mark - 下面弹出登陆
    [askBtn addTarget:self action:@selector(denglu:) forControlEvents:UIControlEventTouchUpInside];
    // MARK: - 5.记录成员变量
    _scrollView = scrollV;
    _headview = headView;
    _categoryView = categoryV;
    _detailView = detailView;
}
-(void)denglu:(UIButton *)Btn
{
    NSLog(@"dengu");
    HMDengLuViewController *dengluVC = [[HMDengLuViewController alloc] init];
    [self.navigationController pushViewController:dengluVC animated:YES];
    
    
}
-(void)askDoctor:(UIButton *)btn
{
//    NSLog(@"咨询医师");
    YZAskViewController *askViewController = [[YZAskViewController alloc] init];
    [askViewController setImage:[UIImage imageNamed:self.imageStr]] ;
    [self.navigationController pushViewController:askViewController animated:YES];
    
//    [self.navigationController pushViewController:[[YZAskViewController alloc] init] animated:YES];
    
}
-(void)setModel:(YKDoctorModel *)model
{

    _model = model;
    _imageStr = model.icon;
    _name = model.xing;
    _job = model.name;
   
    
    [self setupUI];
}
#pragma mark - 负责添加底部控制器视图的方法,并返回scrollView
- (UIScrollView *)setupContentView {
    
    UIScrollView *scrollV = [[UIScrollView alloc] init];
    scrollV.backgroundColor = [UIColor whiteColor];
    scrollV.showsHorizontalScrollIndicator = NO;
    scrollV.pagingEnabled = YES;
    
    // 设置代理!
    scrollV.delegate = self;
    
    // MARK: - 添加控制器的视图
    NSArray<NSString *> *clsNameArr = @[@"YZappointmentViewController", @"YZFlowersViewController", @"YZDoctorTimeViewController"];
    
    // 定义可变数组,负责存放底部控制器视图的view!
    NSMutableArray<UIView *> *tempArrM = [NSMutableArray array];
    
    [clsNameArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        // 根据字符串动态的创建类 & 对象
        Class cls = NSClassFromString(obj);
        
        UIViewController *vc = [[cls alloc] init];
        
        NSAssert([vc isKindOfClass:[UIViewController class]], @"%@ 控制器类名 写的不对!", obj);
        
        // 添加控制器视图!
        [self cz_addChildController:vc intoView:scrollV];
        
        // 添加view
        [tempArrM addObject:vc.view];
    }];
    
    // 布局控制器的视图
    // 三个摆开,x!
    [tempArrM mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    // y size!
    [tempArrM mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.equalTo(scrollV);
        
        // 顶部距离 + 底部距离 + 自己的高度 才能够决定contentSize的高度!
        make.top.bottom.equalTo(scrollV);
        
    }];
    
    return scrollV;
}

#pragma mark - 代理方法
// UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 1.获取内容的偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    
//    NSLog(@"%f", offsetX);
    // 2.设置偏移距离给分类视图
    // 分类视图黄条的移动距离为 offsetX 的 3 分之 1
    _categoryView.offset_x = offsetX / 3;
    
}
#pragma mark - 监听事件
// 分类视图的值改变事件
- (void)categoryViewValuechanged:(YZCategoryView *)sender {
//    NSLog(@"%zd",sender.pageNumber);
    CGRect rect = CGRectMake(_scrollView.bounds.size.width * sender.pageNumber, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height);
    [_scrollView scrollRectToVisible:rect animated:YES];
    
}

#pragma mark - 拖拽手势
- (void)panAction:(UIPanGestureRecognizer *)recognizer {
    
    
    // 1.获取用户的偏移量
    CGPoint translate = [recognizer translationInView:self.view];
    
    // 2.重置
    [recognizer setTranslation:CGPointZero inView:self.view];
    
    //    NSLog(@"%f", translate.y);
    // 3.计算一个高度 -> 获取顶部视图本身的高度 ,进行计算!
    CGFloat height = translate.y + _headview.bounds.size.height;
    
    // MARK: - 计算导航条的透明度!
    // - 计算单位透明度变化量
    CGFloat percent = 1.0 / (kHeaderHeight - 64);
    // - 计算需要的透明度
    CGFloat needAlpha = (height - 64) * percent;
    // - 真正需要的alpha
    CGFloat alpha = 1 - needAlpha;
    
    // - 修改透明度
    self.navigationController.navigationBar.alpha = alpha;
    
    
    // 4.2 不让超出范围 最大就是 123 最小高度 64!
    // 会有问题!
    //    if (height > kHeaderHeight || height < 64) {
    //        return;
    //    }
    // 完善做法
    if (height > kHeaderHeight) {
        height = kHeaderHeight;
    }
    if (height < 64) {
        height = 64;
    }
    
    // 4.修改顶部视图的高度
    [_headview mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(height);
    }];
    
    // 5.布局子控件
    // layoutIfNeeded 强制视图重新布局自己的子控件!
    // 注意:如果需要做动画,那就必须使用 layoutIfNeeded
    [self.view layoutIfNeeded];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
